Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article provides supplementary remarks to the reference documentation for this API.
The HashSet<T> class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
The capacity of a HashSet<T> object is the number of elements that the object can hold. A HashSet<T> object's capacity automatically increases as elements are added to the object.
The HashSet<T> class is based on the model of mathematical sets and provides high-performance set operations similar to accessing the keys of the Dictionary<TKey,TValue> or Hashtable collections. In simple terms, the HashSet<T> class can be thought of as a Dictionary<TKey,TValue> collection without values.
A HashSet<T> collection is not sorted and cannot contain duplicate elements. If order or element duplication is more important than performance for your application, consider using the List<T> class together with the Sort method.
HashSet<T> provides many mathematical set operations, such as set addition (unions) and set subtraction. The following table lists the provided HashSet<T> operations and their mathematical equivalents.
HashSet operation | Mathematical equivalent |
---|---|
UnionWith | Union or set addition |
IntersectWith | Intersection |
ExceptWith | Set subtraction |
SymmetricExceptWith | Symmetric difference |
In addition to the listed set operations, the HashSet<T> class also provides methods for determining set equality, overlap of sets, and whether a set is a subset or superset of another set.
.NET Framework only: For very large HashSet<T> objects, you can increase the maximum capacity to 2 billion elements on a 64-bit system by setting the enabled
attribute of the <gcAllowVeryLargeObjects>
configuration element to true
in the run-time environment.
The HashSet<T> class implements the ISet<T> interface.
HashSet and LINQ set operations
LINQ provides access to the Distinct
, Union
, Intersect
, and Except
set operations on any data source that implements the IEnumerable or IQueryable interfaces. HashSet<T> provides a larger and more robust collection of set operations. For example, HashSet<T> provides comparisons such as IsSubsetOf and IsSupersetOf.
The primary difference between LINQ set operations and HashSet<T> operations is that LINQ set operations always return a new IEnumerable<T> collection, whereas the HashSet<T> equivalent methods modify the current collection.
Typically, if you must create a new set or if your application needs access only to the provided set operations, using LINQ set operations on any IEnumerable<T> collection or array will be sufficient. However, if your application requires access to additional set operations, or if it is not desirable or necessary to create a new collection, use the HashSet<T> class.
The following table shows the HashSet<T> operations and their equivalent LINQ set operations.
HashSet operation | LINQ equivalent |
---|---|
UnionWith | Union |
IntersectWith | Intersect |
ExceptWith | Except |
Not provided. | Distinct |
SymmetricExceptWith | Not provided. |
Overlaps | Not provided. |
IsSubsetOf | Not provided. |
IsProperSubsetOf | Not provided. |
IsSupersetOf | Not provided. |
IsProperSupersetOf | Not provided. |
SetEquals | Not provided. |